package com.janlei.tdd;


import java.util.Optional;
import java.util.function.IntConsumer;
import java.util.stream.IntStream;

public class Dynamic {

    int fibonacci(int step) {

        int n = getLen(step);
        int[] dp = initDp(n);

        IntStream.range(2, n)
                .forEach(getConsumer(dp));

        return dp[n - 1];
    }



    private int getLen(int step) {
        return  Optional.of(step)
                .filter(num -> num > 1)
                .orElse(2);
    }

    private IntConsumer getConsumer(int[] dp) {
        return i -> dp[i] = dp[i - 1] + dp[i - 2];
    }

    private int[] initDp(int len) {
        int[] dp = new int[len];

        dp[0] = 1;
        dp[1] = 1;
        return dp;
    }
}
